#!/usr/bin/env python

# ------------------------------------
# projects/python/matlab/TestMatLab.py
# Copyright (C) 2009
# Yersain Ospanov
# ------------------------------------

# To run the tests
#     TestMatLab.py

# To document the tests
#     pydoc -w TestMatLab

import MatLab
import unittest

# ----------
# TestMatLab
# ----------

class TestMatLab (unittest.TestCase) :
    # --------
    # add
    # --------

    def test_add1 (self) :
        m = ((2, 3, 4), (5, 6, 7))
        n = ((2, 3, 4), (5, 6, 7))
        self.assert_(MatLab.add(m,n) == ((4, 6, 8),(10, 12, 14)))

    def test_add2 (self) :
        m = ((2, 3, 4), (5, 6, 7))
        n = 2
        self.assert_(MatLab.add(m,n) == ((4, 5, 6),(7, 8, 9)))

    def test_add3 (self) :
        n = ((2, 3, 4), (5, 6, 7))
        m = 2
        self.assert_(MatLab.add(m,n) == ((4, 5, 6),(7, 8, 9)))

    # --------
    # subtract
    # --------

    def test_subtract1 (self) :
        m = ((2, 3, 4), (5, 6, 7))
        n = ((1, 3, 2), (2, 3, 9))
        self.assert_(MatLab.subtract(m,n) == ((1, 0, 2),(3, 3, -2)))

    def test_subtract2 (self) :
        m = ((2, 3, 4), (5, 6, 7))
        n = 2
        self.assert_(MatLab.subtract(m,n) == ((0, 1, 2),(3, 4, 5)))

    def test_subtract3 (self) :
        n = ((2, 3, 4), (5, 6, 7))
        m = 10
        self.assert_(MatLab.subtract(m,n) == ((-8, -7, -6),(-5, -4, -3)))


    # --------
    # sum_cols
    # --------

    def test_sum_cols (self) :
        m = ((2, 3, 4), (5, 6, 7))
        self.assert_(MatLab.sum_cols(m) == (7, 9, 11))

    # --------
    # sum_rows
    # --------

    def test_sum_rows (self) :
        m = ((2, 3, 4), (5, 6, 7))
        self.assert_(MatLab.sum_rows(m) == (9, 18))


    # --------
    # sum_rows
    # --------

    def test_sum_rows (self) :
        m = ((2, 3, 4), (5, 6, 7))
        self.assert_(MatLab.sum_rows(m) == (9, 18))

    # --------
    # zeros
    # --------

    def test_zeros (self) :
        r = 3
        c = 2
        self.assert_(MatLab.zeros(r,c) == ((0,0),(0,0),(0,0)))

    # --------
    # ones
    # --------

    def test_ones (self) :
        r = 2
        c = 3
        self.assert_(MatLab.ones(r,c) == ((1,1,1),(1,1,1)))

    # --------
    # cth column
    # --------

    def test_slice_column1 (self) :
        m = ((2,1,3),(2,1,1))
        c = 2
        self.assert_(MatLab.slice_col(m, c)== (3,1))

    def test_slice_column2 (self) :
        m = ((2,1,3),(2,1,1),(2,1,3),(2,1,1))
        c = 1
        self.assert_(MatLab.slice_col(m, c)== (1,1,1,1))
        
    # --------
    # rth row
    # --------

    def test_slice_row1 (self) :
        m = ((2,1,3),(2,1,1))
        r = 1
        self.assert_(MatLab.slice_row(m, r)== (2,1,1))

        
    # --------
    # transpose
    # --------

    def test_transpose1 (self) :
        m = ((2,1,3),(2,1,1))
        self.assert_(MatLab.transpose(m)== ((2,2),(1,1),(3,1)))   
 
    # --------
    # concat_cols
    # --------

    def test_concat_cols1 (self) :
        m = ((2,1,3),(2,1,1))
        n = ((2,1,3),(2,1,1))
        self.assert_(MatLab.concat_cols(m, n)== 
((2,1,3),(2,1,1),(2,1,3),(2,1,1)))

    # --------
    # concat_rows
    # --------

    def test_concat_rows1 (self) :
        m = ((2,1,3),(2,1,1))
        n = ((2,1,3),(2,1,1))
        self.assert_(MatLab.concat_rows(m, n)== ((2,1,3,2,1,3),(2,1,1,2,1,1)))

    def test_concat_rows2 (self) :
        m = ((2,1,3),(2,1,1),(2,1,3),(2,2,2))
        n = ((2,1,3),(2,1,1),(2,1,3),(2,1,1))
        self.assert_(MatLab.concat_rows(m, 
n)==((2,1,3,2,1,3),(2,1,1,2,1,1),(2,1,3,2,1,3),(2,2,2,2,1,1)))

    # --------
    # eye
    # --------

    def test_eye1 (self) :
        r = 3
        c = 2
        self.assert_(MatLab.eye(r,c) == ((1,0),(0,1),(0,0)))
        
    def test_ey2 (self) :
        r = 3
        c = 3
        self.assert_(MatLab.eye(r,c) == ((1,0,0),(0,1,0),(0,0,1)))         
    
    # --------
    # diag
    # --------

    def test_diag1 (self) :
        m=((1,2,3),(4,5,6),(7,8,9))
        self.assert_(MatLab.diag(m) == ((1,5,9)))
        
    def test_diag2 (self) :
        m=((1,2,3),(4,5,6))
        self.assert_(MatLab.diag(m) == ((1,5)))
        
    def test_diag3 (self) :
        m=((1,2,3),(4,5,6),(1,2,3),(4,5,6))
        self.assert_(MatLab.diag(m) == ((1,5,3)))
        
    # --------
    # multiply
    # --------

    def test_multiply1 (self) :
        m = ((2, 3, 4), (5, 6, 7))
        n = 2
        self.assert_(MatLab.multiply(m,n) == ((4, 6, 8),(10, 12, 14)))

    def test_multiply2 (self) :
        n = ((2, 3, 4), (5, 6, 7))
        m = -1
        self.assert_(MatLab.multiply(m,n) == ((-2, -3, -4),(-5, -6, -7)))

    def test_multiply3 (self) :
        m = ((1, 2), (3, 4))
        n = ((5, 6), (7, 8))
        self.assert_(MatLab.multiply(m,n) == ((19, 22),(43, 50)))
    
    def test_multiply4 (self) :
        m = ((1, 2, 3), (3, 4, 4))
        n = ((5, 6), (7, 8), (1,2))
        self.assert_(MatLab.multiply(m,n) == ((22, 28),(47, 58)))
    
    def test_multiply5 (self) :
        m = ((5, 6), (7, 8), (1,2))
        n = ((1, 2, 3), (3, 4, 4))
        self.assert_(MatLab.multiply(m,n) == ((23, 34, 39), (31, 46, 53), (7, 10, 11)))
            
    # --------
    # tril
    # --------

    def test_tril1 (self) :
        m = ((2, 3, 4), (5, 6, 7),(5, 6, 7))
        self.assert_(MatLab.tril(m) == ((0, 0, 0),(5, 0, 0),(5, 6, 0)))

    def test_tril2 (self) :
        m = MatLab.ones(4, 4)
        self.assert_(MatLab.tril(m) == ((0, 0, 0, 0),(1, 0, 0, 0),(1, 1, 0, 0), (1, 1, 1, 0)))

    # --------
    # triu
    # --------

    def test_triu1 (self) :
        m = ((2, 3, 4), (5, 6, 7),(5, 6, 7))
        self.assert_(MatLab.triu(m) == ((0, 3, 4),(0, 0, 7),(0, 0, 0)))

    def test_triu2 (self) :
        m = MatLab.ones(4, 4)
        self.assert_(MatLab.triu(m) == ((0, 1, 1, 1),(0, 0, 1, 1),(0, 0, 0, 1), (0, 0, 0, 0)))
            
if __name__ == "__main__" :
    unittest.main()
